﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Database.Context;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Biz
{
    public class UserBiz : BaseBiz
    {
        public UserBiz()
        {
            DbContext = new Context();
        }

        public UserBiz(Context context)
        {
            DbContext = context;
        }

        public override void Insert(Model.BaseModel model)
        {
            var user = model as Model.User;
            user.Created = DateTime.Now;
            user.Modified = DateTime.Now;
            user.CreatedBy = Utilities.CurrentUser();
            user.ModifiedBy = Utilities.CurrentUser();

            DbContext.Users.Add(user);
            DbContext.SaveChanges();
        }

        public override void Update(Model.BaseModel model)
        {
            var user = model as Model.User;
            user.Modified = DateTime.Now;
            user.ModifiedBy = Utilities.CurrentUser();

            DbContext.Entry(user).State = EntityState.Modified;
            DbContext.SaveChanges();
        }

        public override void Delete(Model.BaseModel model)
        {
            var m = model as Model.User;
            var user = DbContext.Users.FirstOrDefault(i => i.Id == m.Id);
            DbContext.Users.Remove(user);
            DbContext.SaveChanges();
        }

        public override void DeleteItems(List<Model.BaseModel> models)
        {
            throw new NotImplementedException();
        }

        #region GetItemById

        public override Model.BaseModel GetItemById(int id)
        {
            var model = (from item in DbContext.Users
                         where item.Id == id
                         select item).First();
            return model;
        }

        #endregion

        #region GetAll

        public List<Model.User> GetAll()
        {
            return DbContext.Users.OrderBy(u => u.Username).ToList();
        }

        #endregion

        public bool CheckUserLogin(string account, string password)
        {
            bool isValid = false;

            if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(password))
            {
                var user = GetItemByAccount(account);
                if (user != null)
                {
                    var decodedPassword = Utilities.DecodedPassword(user.Password);
                    if (string.Equals(decodedPassword, password, StringComparison.InvariantCultureIgnoreCase))
                    {
                        isValid = true;
                    }
                }
            }

            return isValid;
        }

        public Model.User GetItemByAccount(string account)
        {
            var result = (from item in DbContext.Users
                          where item.Username == account
                          select item);
            if (result.Count() > 0)
            {
                return result.First();
            }
            return null;
        }
    }
}